package com.matrix.java.chap_07.example;

import com.matrix.java.chap_06.example.Case6_10;

public class Case7_04 {
    public static void main(String[] args) {
        char[] chars = createArray();
        System.out.println("The lowercase letters are: ");
        displayArray(chars);
        int[] counts = countLetters(chars);
        System.out.println();
        System.out.println("The occurrences of each letter are:");
        displayCounts(counts);
    }

    private static void displayCounts(int[] counts) {
        for (int i = 0; i < counts.length; i++) {
            if ((i + 1) % 10 == 0) {
                System.out.println(counts[i] + "\t" + (char) (i + 'a'));
            } else {
                System.out.print(counts[i] + "\t" + (char) (i + 'a') + "\t");
            }
        }
    }

    private static int[] countLetters(char[] chars) {
        int[] counts = new int[26];
        for (int i = 0; i < chars.length; i++) {
            counts[chars[i] - 'a']++;
        }
        return counts;
    }

    private static void displayArray(char[] chars) {
        for (int i = 0; i < chars.length; i++) {
            if ((i + 1) % 20 == 0) {
                System.out.println(chars[i]);
            } else {
                System.out.print(chars[i] + "\t");
            }
        }
    }

    private static char[] createArray() {
        char[] chars = new char[100];
        for (int i = 0; i < chars.length; i++) {
            chars[i] = Case6_10.getRandomLowerCaseLetter();
        }
        return chars;
    }
}
/*
public class Case7_04 {
    public static void main(String[] args) {
        char[] arr = new char[100];
        char[] letterArr = new char[26];
        int[] counts = new int[26];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = Case6_10.getRandomLowerCaseLetter();
            System.out.print(arr[i] + "\t");
        }
        for (int i = 0, j = 'a'; i < letterArr.length; i++, j++) {
            letterArr[i] = (char) j;
        }
        for (char c : arr) {
            int num = getLetter(c, letterArr);
            counts[num]++;
        }
        System.out.println();
        for (int i = 0; i < counts.length; i++) {
            System.out.print(counts[i] + "\t");
        }
    }

    private static int getLetter(char c, char[] letterArr) {
        int num = 0;
        for (int i = 0; i < letterArr.length; i++) {
            if (c == letterArr[i]) {
                num = i;
                break;
            }
        }
        return num;
    }
}
*/
